using System;
using System.Text;

namespace TjingMe.Web.Models
{
	public class EventRepository
	{
		public EventRepository ()
		{
		}
		
		public Event GetEvent(string id){
			return System.Web.HttpContext.Current.Session[id] as Event;
		}
		
		public void Save(Event ev){
			ev.Id = Guid.NewGuid().ToString();
			var itemsList = new ItemsList();
			itemsList.Id = Guid.NewGuid().ToString();
			itemsList.Name = "Lista";
			itemsList.EventKey = ev.Key;
			for(var i=0;i<ev.Name.Length; i++){
				itemsList.Items.Add(new Item{Id = Guid.NewGuid().ToString(), Name = CreateKey()});
			}
			ev.ItemLists.Add(itemsList);
			System.Web.HttpContext.Current.Session[ev.Key] = ev;
		}
		
		public static string CreateKey(){
			return Creator.RandomString(8);
		}
		
	}
	
	public class Creator{
		
		private static Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden
		public static string RandomString(int size)
		    {
		        StringBuilder builder = new StringBuilder();
		        char ch;
		        for (int i = 0; i < size; i++)
		        {
		            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));                 
		            builder.Append(ch);
		        }
		
		        return builder.ToString();
		    }

			
				}
}

